﻿@{
    ViewBag.Title = "Ins";
}
@section scripts
{   
    <script src="@Url.Content("~/Scripts/shared/share.js")" type="text/javascript"></script> 
    <script src="@Url.Content("~/Scripts/shared/shown.js")" type="text/javascript"></script>  
    <script src="@Url.Content("~/Scripts/Bill/Bill.js?v=GetDate()")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/common/Print.js")" type="text/javascript"></script>    
}
<body>
   <form id="form1" runat="server" method="post" style="height: 100%">
    
    <div class="mini-toolbar" style="padding:2px;border-top:0;border-left:0;border-right:0;"> 
        <table width = "1000px;">
            <tr>
                <td width="1000px;">
                    <a class="mini-button" id="new" name="new" iconCls="icon-new" onclick="NewBill" plain="true">新增</a>
                    <a class="mini-button" id="save" name="save" iconCls="icon-save" onclick="saveBill" plain="true">保存</a>
                    <span class="separator"></span>
                    <a class="mini-button" id="addrow" name="addrow" iconCls="icon-addnew" onclick="addRow" plain="true">增行</a>
                    <a class="mini-button" id="delrow" name="delrow" iconCls="icon-remove" onclick="removeRow" plain="true">删行</a>
                    <span class="separator"></span>
                    <a class="mini-button" id="modify" name="modify" iconCls="icon-edit" onclick="modify" plain="true">改单</a>
                    <a class="mini-button" id="check" name="check" iconCls="icon-ok" onclick="checkBill" plain="true">审核</a>
                    @*<a class="mini-button" id="Approve" name="Approve" iconCls="icon-ok" onclick="ApproveBill" plain="true">审批</a>*@
                    <a class="mini-button" id="RefundApprove" name="RefundApprove" iconCls="icon-ok" onclick="ApproveBill" plain="true" enabled="false">退款审核</a>
                    <span class="separator"></span>
                    <a class="mini-button" id="print" iconCls="icon-print" onclick="printBillMyseft" plain="true">打印</a>
                    <a class="mini-button" id="getdata" iconCls="icon-collapse" onclick="getBillData" plain="true">浏览</a>
                    <span class="separator"></span>
                    <a class="mini-button" id="close" iconCls="icon-close" onclick="CloseWindowBill" plain="true">关闭</a>
                </td>
            </tr>
        </table>                 
    </div>    
    <div id = "DivHeader">
        <table style="width:700px;">
            <tr>
                <td width ="80px" align="right">订单会员：</td>
                <td width ="190px">    
                    @Html.Partial("~/Views/Modular/LookupWeixinInfo.cshtml")                       
                </td>
                <td width ="80px" align="right">开单日期：</td>
                <td width ="120px">    
                    <input name="FDate" class="mini-datepicker" style="width:120px;"/>
                </td>
                 <td width ="80px" align="right">单据编号：</td>
                 <td width ="150px">    
                    <input name="FBillNo" class="mini-textbox" readonly="readonly"style="width:150px;"/>
                 </td>
            </tr>
            <tr>
                <td align="right">门店名称：</td>
                <td >
                    @Html.Partial("~/Views/Modular/LookupStockInfo.cshtml")
                </td>
                <td align="right">单据备注：</td>                    
                <td >  
                    <input name="FRemark" class="mini-textbox"  style="width:120px;"/>  
                </td>
                <td align="right">审核状态：</td>
                <td >    
                    <input name="txtCheckFlag" class="mini-textbox" readonly="readonly"style="width:150px;"/></td>
            </tr>
        </table>
    </div>
    <div class="mini-fit">
        <div id="datagrid1" class="mini-datagrid" style="width:100%;height:100%;" 
            showPager = "false" showPageSize="false" showPageIndex="false" skipReadOnlyCell="true"
            allowCellSelect="true" allowSortColumn = "false" allowCellEdit="true" navEditMode="true"
            editNextOnEnterKey="true"  editNextRowCell="false" showSummaryRow="true"
            ondrawcell="onDrawCell" oncellcommitedit="onCellCommitEdit" ondrawsummarycell="onDrawSummaryCell"
            onrowdblclick="onRowDblClick" fitColumns="false" oncelleditenter="onCelleditenter">
            <div property="columns">            
                <div type="indexcolumn" width="35" headerAlign="center" >序号</div>
                <div field="mFNumber" name="mFNumber" displayField = "mFNumber" width="120" headerAlign="center">商品编码
                    <input property="editor" class="mini-buttonedit" onbuttonclick="onButtonEdit" onvaluechanged="onValueChanged" style="width:100%;" />
                </div>  
                <div field="mFName" name="mFName" width="160" headerAlign="center">商品名称</div>  
                <div field="mFModel" name="mFModel" width="100" headerAlign="center">规格型号</div>
                <div field="mFUnitName" name="mFUnitName" width="40" headerAlign="center">单位</div>
                <div field="mFConvert" name="mFConvert" width="70" headerAlign="center" align="right">内装数</div>
                <div field="mFQty" name="mFQty" width="70" headerAlign="center" align="right">数量
                    <input property="editor" class="mini-spinner" maxValue="99999999" style="width:100%;"changeOnMousewheel ="false" showbutton="false" increment ="0" />
                </div>
                <div field="mFPrice" name="mFPrice" width="70" numberFormat="#,0.00" headerAlign="center" align="right">单价
                    <input property="editor" class="mini-spinner" maxValue="99999999" style="width:100%;"changeOnMousewheel ="false" showbutton="false" increment ="0" />
                </div>
                <div field="mFAmount" name="mFAmount" numberFormat="#,0.00" width="90" headerAlign="center" align="right" summaryType="sum">金额
                <input property="editor" class="mini-spinner" maxValue="99999999" style="width:100%;"changeOnMousewheel ="false" showbutton="false" increment ="0" />
                </div>
                <div field="mFCode" name="mFCode" width="120" headerAlign="center">商品条码</div>                
                <div field="mFRemark" name="mFRemark" headerCls="CellLast" width="150" headerAlign="center" >备注
                    <input property="editor" class="mini-textbox" style="width:100%;"/>
                </div>
            </div>
        </div>  
    </div>

     @Html.Partial("~/Views/Modular/Bill/MallFooter.cshtml")
       
    </form>

     <input id="type" name="type" value="@ViewBag.type" type="hidden"/>

    <script type="text/javascript">

        mini.parse();
        var form = new mini.Form("form1");
        var grid = mini.get("datagrid1");      
        var uID = parent.window.getSystemAppID();
        var uCode = parent.window.getSystemAppCode();
        var uName = parent.window.getSystemAppName();
        var sumFields = "mFQty,mFSetQty,mFAmount";
        var id = getQueryString("billid");
        if (id != null) {
            mini.getbyName("KInterID").setValue(id);
        }        
        var datatype = getQueryString("datatype");      

        LoadData();

        function newBills() {
            NewBill();
            var nowDate = new Date();
            mini.getbyName("FBillDate").setValue(nowDate);
        }

        function LoadData() {
            var minterid = mini.getbyName("KInterID").value;
            $.ajax({
                url: '../Bill/GetBillData',
                data: { mSystemAppID: uID, mBillID: minterid, mDataType: datatype },
                dataType: 'text',
                async: false,
                success: function (text) {
                    var dataR = mini.decode(text);
                    grid.setData(dataR.datadetail);
                    if (dataR.datamaster.length > 0) {
                        var mdata = dataR.datamaster[0];
                        form.setData(mdata);
                        mini.getbyName("FWeixinID").setValue(mdata.FWeixinID);
                        mini.getbyName("FWeixinID").setText(mdata.FWeixinNumber);
                        mini.getbyName("FStockID").setValue(mdata.FStockID);
                        mini.getbyName("FStockID").setText(mdata.FStockNumber);
                        mini.getbyName("comBillFlag").setValue(mdata.FStateID);
                        //mini.getbyName("comPayFlag").setValue(mdata.FPayFlag);
                        if (mdata.FCheckFlag == 1) {
                            BillState(2);
                        } else if (mdata.FStateID == 1) {
                            BillState(1);
                            try {
                                mini.get("modify").setEnabled(false);
                                mini.get("RefundApprove").setEnabled(false);
                            } catch (e) { }
                        }
                        else{
                            BillState(1);
                        }
                        if (mdata.FApproveFlag == 1) {
                            BillState(5);
                        }
                        if ((mdata.KInterID == "0") || (mdata.KInterID == null)) {
                            newBills();
                        }
                        
                        if (mdata.FStateID == 3) {
                            BillState(2);
                            mini.get("RefundApprove").setEnabled(true);
                        }
                        else if (mdata.FStateID != 1){
                            BillState(0);
                            try{
                                mini.get("addrow").setEnabled(false);
                                mini.get("delrow").setEnabled(false);
                                mini.get("ScanAdd").setEnabled(false);
                            } catch (e) { }
                        }
                    }
                    else {
                        newBills();
                    }
                }
            });
        }

        function checkValid() {
            if ((mini.getbyName("FBillerID").getValue() == "") || (mini.getbyName("FBillerID").getValue() == "0")) {
                var newbillerid = parent.window.getSystemAppID();
                mini.getbyName("FBillerID").setValue(newbillerid);
            }

            var MainData = form.getData();
            if (MainData.FWeixinID.toString() == "") {
                mini.showTips({ content: "<b>保存失败</b> <br/>会员名称不能空值!", state: "danger", x: "center", y: "top", timeout: 3000 });
                return false;
            }
            if (MainData.FStockID.toString() == "") {
                mini.showTips({ content: "<b>保存失败</b> <br/>门店不能空值!", state: "danger", x: "center", y: "top", timeout: 3000 });
                return false;
            }
            return true;
        }

        function saveBill() {
            BillState(1);//先锁定
            if (checkValid() == false) {
                BillState(4);//先锁定
                return;
            }
            var MainData = form.getData();
            var cDate = mini.getbyName("FDate").getFormValue();
            var rCnt = grid.getData().length;
            var sBillNo = MainData.FBillNo;

            var mjson = "[{'KInterID':'" + MainData.KInterID + "',";
            mjson = mjson + "'FBillNo':'" + MainData.FBillNo + "',";
            mjson = mjson + "'FSystemAppID':'" + uID + "',";
            mjson = mjson + "'FWeixinID':'" + MainData.FWeixinID + "',";
            mjson = mjson + "'FStockID':'" + MainData.FStockID + "',";
            mjson = mjson + "'FDate':'" + cDate + "',";
            mjson = mjson + "'FDataType':'" + datatype + "',";
            mjson = mjson + "'FRemark':'" + MainData.FRemark + "',";
            mjson = mjson + "'FStateID':'" + MainData.comBillFlag + "',";
            var json = "'goods_list':[";
            for (var i = 0; i < rCnt; i++) {
                var rs = grid.getRow(i);
                if (typeof (rs.mFNumber) == "undefined") continue;
                js = "{"              
                js = js + '"mFItemID":' + rs.mFItemID + ",";
                js = js + '"mFUnitName":"' + rs.mFUnitName + '",';
                js = js + '"mFConvert":"' + rs.mFConvert + '",';
                var qty = typeof (rs.mFQty) == "undefined" ? 0 : rs.mFQty;
                js = js + '"mFQty":' + qty + ",";
                var price = typeof (rs.mFPrice) == "undefined" ? 0 : rs.mFPrice;
                js = js + '"mFPrice":' + price + ",";
                var money = typeof (rs.mFAmount) == "undefined" ? 0 : rs.mFAmount;
                js = js + '"mFAmount":' + money + ",";
                var remark = typeof (rs.mFRemark) == "undefined" ? "" : rs.mFRemark;
                js = js + '"mFRemark":"' + remark + '"';
                js = js + "}"
                json = json + js + ","

            }
            json = json.substring(0, json.length - 1);
            json = json + "]";
            if (json.length < 8) {
                mini.alert("没有要保存的数据！");
                return;
            }
            mjson = mjson + json + "}]";
            $.ajax({
                url: '../Bill/SaveBillData_Mall',
                data: { mdatajson: mjson },
                type: "post",
                cache: false,
                success: function (txt) {
                    var ret = mini.decode(txt);
                    if (ret.id != -1) {
                        mini.getbyName("KInterID").setValue(ret.id);
                        mini.getbyName("FBillNo").setValue(ret.FBillNo);
                        BillState(1);
                    }
                    else {
                        mini.alert("异常:" + ret.Status);
                    }
                },
                error: function (xhr) {
                    mini.showTips({ content: "网络异常!", state: "danger", x: "center", y: "top", timeout: 3000 });
                }
            });
        }

        function printBill() {
            var url = "../Bill/PrintBill?mSystemAppID=" + parent.window.getSystemAppID() + "&mBillID=" + mini.getbyName("KInterID").value + "&mDataType=" + $("#type").val();
            mini.open({
                url: url,
                title: "打印", width: 1000, height: 700
            });
        }

        function onChanged() {
            var MainData = form.getData();
            var mjson = "[{'KInterID':'" + MainData.KInterID + "',";
            mjson = mjson + "'FPayFlag':" + MainData.comPayFlag + ",";
            mjson = mjson + "'mSystemAppID':'" + uID + "'";
            mjson = mjson + "}]";

            $.post("../bill/setPayFlag", { json: mjson }, function (data) {
                debugger;
                var ret = mini.decode(data);
                if (ret.status != 1) {
                    mini.alert("异常:" + xhr.responseText);
                }
                else {
                    mini.showTips({ content: "修改完成!", state: "success", x: "center", y: "top", timeout: 3000 });
                }
            });
        }

        function ApproveBill() {
            mini.confirm("确定审核退款吗？", "提示",
                function (action) {
                    if (action == "ok") {
                        setApproveBill();
                    }
                }
            );
        }

        function setApproveBill() {
            var kid = mini.getbyName("KInterID").value;
            var json = "[{";
            json = json + "'KInterID':'" + kid + "',";
            json = json + "'FDataType':'" + datatype + "',";
            json = json + "'FBillState':'" + mini.getbyName("comBillFlag").getValue() + "',";
            json = json + "'FSystemAppID':'" + uID + "'}]";
            $.ajax({
                url: '../Bill/RefundCheckBillData',
                data: { mdatajson: json },
                type: "post",
                cache: false,
                success: function (txt) {
                    var ret = mini.decode(txt);
                    mini.showMessageBox({
                        showModal: false, width: 250, title: "提示", iconCls: "mini-messagebox-info",
                        message: ret.Status, timeout: 3000, x: "right", y: "bottom"
                    });

                    if (ret.id != -1) {
                        mini.getbyName("KInterID").setValue(ret.id);
                        BillState(5);
                    }
                    else {
                        mini.alert("异常:" + ret.Status);
                    }
                },
                error: function (xhr) {
                    mini.alert("异常:" + xhr.responseText);
                }
            });
        }

        function Query() {
            var json = "[{";
            json = json + "'BillNo':'" + billno + "',";
            json = json + "'EntryID':'" + $("#EntryID").val() + "',";
            json = json + "'mSystemAppID':'" + parent.window.getSystemAppID() + "'}]";
            //主动查询
            $.post("../AppCenter/QueryPay", { mjson: json }, function (data) {
        });
    }

        Query();

    </script>
</body>